#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QPushButton>

#include <QtGui>
#include <QtCore>

#include "pageresult.h"
#include "pages.h"
#include "pagetestconfig.h"
#include "pageerrortest.h"
#include "pageautoverify.h"

#include "cappacket.h"
#include "udp.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    QProcess packet_analyzer;

    float wcbc_array[6][8];
    void read_wcbc_from_db();

    QWidget *widgetInfo;
    QLabel *labInfo;
    QProgressBar *barTestProcess;

    int showControlFlag;

    double harmonic_std[42];
    double harmonic_dut[42];
    double harmonic_total;
    double harmonic_std_total;
    double harmonic_dut_total;

    void paintHarmonic();
    void analyzeHarmonic();

    int m_iDelayNum;
    double delayMin;
    double delayMax;
    double delayAvg;
    float lsMin;
    float lsMax;
    double m_dblBwAll;
    double m_iBwLost;
    double m_iBwError;

    double dds;
    double dsstemp;
    float dsMax;
    float ssMax;
    int iDsFlag; //0 对时,1 守时
    int iDsNum;
    int iSsNum;
    int iYsNum;
    double jdys_avg;

    QTimer *dsTimer;
    QTimer *ysTimer;

    int iDsNum_static;

private:
    int dataCount;

    QSqlQuery tbWcxx;
    QSqlQuery tbWcdata;

    QTimer *delShowtimer;

    void showWcdata();
    void showNull();

private:
    pageAutoVerify *pageAuto;
    double UaErr,UbErr,UcErr;
    double IaErr,IbErr,IcErr;
    double UaErr_p,UbErr_p,UcErr_p;
    double IaErr_p,IbErr_p,IcErr_p;

    int jingdu_count;

    QWidget *autotestMask;
    QLabel *labAutotestMask;
    QPushButton *btnAutotestMask;

    QLabel *labAutoTestU;
    QLabel *labAutoTestI;
    QLabel *labAutoTestCount;

private:
    Ui::MainWindow *ui;
    void layoutSetup();

    QStackedWidget *stackedPages;

    resultPage *pageResult;
    SysPage *pageSystem;
    errorTestPage *pageErrorTest;
    BwjyPage *pageBwjy;
    LuboPage *pageLubo;

    configPage *pageConfig;
    QPropertyAnimation *animation1;

    CapPacket *cappacket;
    UdpClient *udp;
    ProjectDB *myDB;
    QTimer *fakePPS;
    int pps_count;
    int source_has_been_reconfiged;

    char *cUdpSend;
    QTimer *captimer_mu;

    int tbsx;

    double ratioError[6];
    double phaseError[6];

    double ratioError_avg[6];
    double phaseError_avg[6];

    double ratioError_rms[6];
    double phaseError_rms[6];

    double ratioError_max[6];
    double phaseError_max[6];

    int error_statistics_count=0;

    bool newSaveCycle;

    void resultFilter();
    double SZLB(int num,double*D_ADD);
    QString infnan(QString s);
    QString showDbl(double dbl);

    void errorStatistics();

    bool newStart;

    double voltagePre;
    double currentPre;

    typedef struct _smv
    {
        QString app;
        QString mac;
        QString type;
        int cypl;
        int asdu;
    }smvItem;

    QList <smvItem> smvList;

    void addPageConfig();
    void loadPortConfig();
    void processMuData();
    void setCSCS(int);
    void saveMuinfo();

    void showInfo(QString str){
        labInfo->setText(str);
        widgetInfo->show();

        QTimer::singleShot(2000, widgetInfo, SLOT(hide()));
    }

    void udpCmd(char type);

    void initTestPara();

public slots:
    void on_btnClose_clicked();
    void on_btnPhoto_clicked();
    void showPageConfig();

    void btn_func_clicked();

    void showPara();

    void findAllSMV();

    void addList(QString app,QString mac,QString type,int cypl,int asdu);

    void scanFinished();

    void startErrorTest();

    void NetTest_mu();

    void jsMutWc();

    void triggerPPS();

    void showAll();

    void settempbuff();

    void changeShowPage();

    void slotBack();

    void bwTest();

    void bwjy_pageChanged();

    void szbSlot();

    void showDelayText();

    void slotDs();

    void repaintSZB();

    void slotLuboData();

    void tTest();

    void scrllCurveScroll();

    //void ChangeX_Text();

    void showLubo();

    void startLubo();

    void saveConfig();

    void showBcdata();

    void dataFirst();

    void dataPrevious();

    void dataNext();

    void dataLast();

    void delWcdata();

    void startTimer();

    void stopTimer();

    void showDelAllMsg();

    void saveWcdata();

    void autoJDError();

    void stopAuto();

private slots:
    void on_btnResult_clicked();
    void on_btnSystem_clicked();

protected:
    bool eventFilter(QObject *obj, QEvent *event);

    void paintBlank(QWidget *w);
    void paintForMu(QWidget *w);
    void paintonSZB(QWidget *w);

    void paintOnAll(QWidget *w);

    void paintOnLubo(QWidget *w);

    void systemLoad(QString bz);

protected:
    double m_dblShowPeriods;
    int m_iPeriodCnt;
    int m_iStartPoint;
};

#endif // MAINWINDOW_H
